package com.igroupes.ovineherd.raw.service.raw;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igroupes.ovineherd.dto.ModelOrder;
import com.igroupes.ovineherd.entity.DataModelTableEntity;
import com.igroupes.ovineherd.mapper.DataModelTableMapper;
import com.igroupes.ovineherd.dto.request.ModelOrderRequest;
import com.igroupes.ovineherd.query.FieldDeleteQuery;
import com.igroupes.ovineherd.query.ModelDataJsonTableQuery;
import com.igroupes.ovineherd.query.TableFieldQuery;
import com.igroupes.ovineherd.util.Requires;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataModelTableService extends ServiceImpl<DataModelTableMapper, DataModelTableEntity> {

    @Value("${model.database-name}")
    private String database;

    public int createTable(TableFieldQuery tableFieldQuery) {
        tableFieldQuery.setDataBase(database);
        return this.getBaseMapper().createTable(tableFieldQuery);
    }

    public int addField(TableFieldQuery tableFieldQuery) {
        if(tableFieldQuery.getFieldList().size() > 0) {
            return this.getBaseMapper().addField(tableFieldQuery);
        }
        return 0;
    }
    public int editField(TableFieldQuery tableFieldQuery){
        return this.getBaseMapper().editField(tableFieldQuery);
    }
    public int deleteField(FieldDeleteQuery fieldDeleteQuery){
        return this.getBaseMapper().deleteField(fieldDeleteQuery);
    }

    public List<ModelDataJsonTableQuery> tableJsonAll(Long userId) {
        Requires.requireNonNull(userId);
        return this.getBaseMapper().tableJsonAll(userId);
    }
    public List<ModelDataJsonTableQuery> tableJsonAll(Long userId, Long versionId) {
        Requires.requireNonNull(userId);
        return this.getBaseMapper().tableJsonAllTables(userId, versionId);
    }
    public ModelDataJsonTableQuery tableJsonInfo(Long tableId) {
        return this.getBaseMapper().tableJsonInfo(tableId);
    }

    public int deleteTable(Long userId, Long versionId){
        Map<String, Long> map = new HashMap<>();
        map.put("userId",userId);
        map.put("versionId", versionId);
        return this.getBaseMapper().deleteTable(map);
    }

    public int order(List<ModelOrder> modelOrders, Long versionId, Long userId) {
        Map map = new HashMap();
        map.put("orders", modelOrders);
        map.put("versionId", versionId);
        map.put("userId", userId);
        return this.getBaseMapper().order(map);
    }
}
